﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using InformationSystem.BLL.Core.Model;
using InformationSystem.BLL.Core.Service;
using System.Text;
using InformationSystem.WebSite.Utility;

namespace InformationSystem.WebSite.Forum
{
    public partial class NewThread : BasePage
    {
        public IForumService ForumService { get; set; }
        protected void Page_Load(object sender, EventArgs e)
        {
            int forumid = 0;
            if (Request.QueryString["forumid"] != null)
            {
                int.TryParse(Request.QueryString["forumid"], out forumid);
            }
            //如果没有有效ID转入默认学院主页
            if (forumid == 0) forumid = base.LoginUser.CollegeID;

            if (IsPostBack)
            {
                CreatThead(forumid);
            }
            else
            {
                College college = ForumService.GetCollegeForumInfo(forumid, base.LoginUser);
                if (college == null) return;
                if (!college.CurrentUserCanVisitForum)
                {
                    return;
                }
                this.CreateFilter(college);
            }
        }

        private void CreatThead(int collegeid)
        {
            //判断基本信息是否正确，类型、主题是否为空
            //组装ThreadInfo对象
            //保存
            int seltypeid = 0;
            int.TryParse(typeid.Value, out seltypeid);
            if (seltypeid == 0 || subject.Value.Trim() == "")
            {
                //跑到发帖错误页面
                return;
            }
            ThreadInfo info = new ThreadInfo();
            info.HideName = anoreply.Checked;
            info.CanReply = !canreply.Checked;
            info.CanReplyHideName = cananoreply.Checked;
            info.ThreadType = seltypeid;
            info.ThreadSubject = StringCheck.GetSafeContent(subject.Value.Trim());
            info.Content = StringCheck.GetSafeContent(brand.Text);
            info.CreateTime = DateTime.Now;
            info.CreatUserID = base.LoginUser.UserID;
            info.CreatUserName = base.LoginUser.UserName;
            info.CheckTime = DateTime.Now;
            info.CheckUserID = "";
            info.CheckUserName = "";
            info.CollegeID = collegeid;
            info.IsApproved = true;
            info.IsOnTop = false;
            info.ReplyCount = 0;
            info.VisitedCount = 0;
            info.LastReplyTime = DateTime.Now;
            info.LastReplyUserID = info.CreatUserID;
            info.LastReplyUserName = info.CreatUserName;
            info.LastReplyHideName = anoreply.Checked;

            bool result = ForumService.CreateThread(info, base.LoginUser);
            if (result)
            {
                Response.Redirect(string.Format("ForumList.aspx?forumid={0}", collegeid));
            }
            else
            {
                Response.Redirect("http://www.baidu.com");
            }

        }

        private void ReadForum()
        { }

        private void CreateFilter(College college)
        {
            if (college.ThreadTypeList != null)
            {
                foreach (var item in college.ThreadTypeList)
                {
                    typeid.Items.Add(new ListItem(item.TypeName, item.TypeID.ToString()));
                }
            }
        }

        protected override bool CommitPermissionCheck()
        {
            throw new NotImplementedException();
        }
    }
}